package com.atguigu.yygh.user.api;

import com.atguigu.yygh.model.user.Patient;
import com.atguigu.yygh.result.Result;
import com.atguigu.yygh.user.service.PatientService;
import com.atguigu.yygh.util.AuthContextHolder;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author zr
 * @date 2021/12/18 21:48
 */
@Api(tags = "就诊人管理")
@RestController
@RequestMapping("/api/user/patient")
public class PatientApiController {
    @Resource
    private PatientService patientService;

    @ApiOperation("获取就诊人列表")
    @GetMapping("auth/findAll")
    public Result findAll(HttpServletRequest request) {
        // 获取用户id  根据user_id 查询多个就诊人
        Long userId = AuthContextHolder.getUserId(request);
        List<Patient> patients = patientService.findAllUserId(userId);
        return Result.ok(patients);
    }

    @ApiOperation("添加就诊人")
    @PostMapping("auth/save")
    public Result savePatient(@RequestBody Patient patient, HttpServletRequest request) {
        // 获取user_id
        Long userId = AuthContextHolder.getUserId(request);
        patient.setUserId(userId);
        patientService.save(patient);
        return Result.ok().message("添加成功");
    }

    @ApiOperation("根据id获取就诊人信息")
    @GetMapping("auth/get/{id}")
    public Result getPatient(@PathVariable Long id) {
        Patient patient = patientService.getPatient(id);
        return Result.ok(patient);
    }

    @ApiOperation("修改就诊人信息")
    @PostMapping("auth/update")
    public Result updatePatient(@RequestBody Patient patient) {
        patientService.updateById(patient);
        return Result.ok().message("修改成功");
    }

    @ApiOperation("删除就诊人")
    @DeleteMapping("auth/remove/{id}")
    public Result deletePatient(@PathVariable Long id) {
        boolean flag = patientService.removeById(id);
        return flag ? Result.ok().message("删除成功") : Result.fail().message("删除失败");
    }

    @ApiOperation(value = "获取就诊人")
    @GetMapping("inner/get/{id}")
    public Patient getPatientOrder(@PathVariable Long id) {
        Patient patient = patientService.getById(id);
        return patient;
    }
}
